<template>
  <div class="app-container">
    <div class="form-container">
      <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="auto">
        <el-form-item label="套餐名称" prop="name">
          <el-input v-model="queryParams.name" size="small" placeholder="请输入套餐名称" clearable />
        </el-form-item>
        <el-form-item label="套餐状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="套餐状态" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="dateRange">
          <el-date-picker
            v-model="dateRange"
            size="small"
            value-format="yyyy-MM-dd"
            style="width:225px"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            clearable
          />
        </el-form-item>
      </el-form>
    </div>
    <div class="btns-group">
      <systemBtns :single="single" has-permi="sysCompanyTemp" />
      <el-button v-hasPermi="['system:sysCompanyTemp:changeStatus']" size="middle" :icon="disabledStatus === '1'?'el-icon-star-on':'el-icon-star-off'" :disabled="single" @click="handleStatusChange">
        {{ disabledContent }}
      </el-button>
    </div>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      class="table-list"
      :class="needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll'"
      :data="tableData"
      stripe
      :row-style="rowClass"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column label="套餐名称" prop="name" min-width="80" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="套餐状态" prop="status" min-width="40" align="center" :show-overflow-tooltip="true" :formatter="statusFormat" />
      <el-table-column label="创建者姓名" prop="createName" min-width="60" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" prop="createTime" min-width="80" align="center" :show-overflow-tooltip="true" />
    </el-table>
    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改套餐对话框 -->
    <addDialog ref="addDialog" />
  </div>
</template>

<script>
import addDialog from './components/addDialog'
import { listComTemp, updateComTempStatus, delComTemp } from '@/api/enterprise/company-temp'
import { mapState } from 'vuex'
// 按钮组件
import systemBtns from '@/components/Common/Btns/systemBtns'
// // 接口功能引入
// import {
//   selectMenusByEnterpriseId
// } from '@/api/system/profile/index.js'
export default {
  name: 'SubCompanyTemp',
  components: {
    systemBtns,
    addDialog
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的行对象
      multipleSelection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 启用/停用显示状态
      disabledStatus: '0',
      disabledContent: '停用',
      currentRow: {},
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      selectRow: [],
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.fristPageSize,
        name: '',
        status: '0'
      },
      // 查询参数中的日期
      dateRange: [],
      // 接收高级设置子组件返回的数据
      highLevelQueryParams: ''
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView
    })
  },
  watch: {
    multipleSelection(data) {
      this.selectRow = []
      if (data.length > 0) {
        data.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('sys_normal_disable').then(res => {
      this.statusOptions = res.data.rows
    })
  },
  methods: {
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listComTemp(this.changeDateRange(this.queryParams, this.dateRange)).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.loading = false
        // this.addTaleData()
      })
    },
    addTaleData() {
      if (this.tableData.length < this.queryParams.pageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (JSON.stringify(selection[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        selection = []
      }
      this.multipleSelection = selection
      // eslint-disable-next-line no-prototype-builtins
      if (this.multipleSelection.length === 1 && this.multipleSelection[0].hasOwnProperty('id')) {
        this.disabledStatus = this.multipleSelection[0].status
        if (this.disabledStatus === '0') {
          this.disabledContent = '停用'
        } else {
          this.disabledContent = '启用'
        }
        this.single = false
      } else {
        this.single = true
      }
      this.ids = selection.map(item => item.id)
      // this.single = selection.length !== 1
      // this.multiple = !selection.length
      if (selection.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(selection.pop())
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      // 重置按钮对高级设置子组件中绑定的model值的操作
      this.dateRange = []
      this.handleQuery()
    },
    // 套餐状态修改
    handleStatusChange() {
      const row = this.multipleSelection[0]
      const text = row.status === '0' ? '停用' : '启用'
      this.$confirm('确认要' + text + '' + row.name + '套餐吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          if (row.status === '0') {
            row.status = '1'
          } else {
            row.status = '0'
          }
          const formdata = {
            'id': row.id,
            'status': row.status
          }
          updateComTempStatus(formdata).then(resp => {
            if (resp) {
              this.getList()
              this.msgSuccess('操作成功！')
            } else {
              this.msgError(resp.message)
            }
          })
        })
        .catch(() => {
        })
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.addDialog.handleAdd('新增分公司套餐')
    },

    /** 修改按钮操作 */
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改分公司套餐', this.multipleSelection[0].id)
    },
    /** 删除按钮操作 */
    handleDelete() {
      let ids = ''
      const child = this.multipleSelection
      child.forEach(item => {
        ids += item.id + ','
      })
      // 去掉最后一个逗号
      if (ids.length > 0) {
        ids = ids.substr(0, ids.length - 1)
      }
      this.$confirm(
        '是否确认删除数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delComTemp(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function() {})
    }
  }
}
</script>
